/** 指定元素全屏 */
export const fullScreen = (element: HTMLDivElement | null | undefined): Promise<void> => {
    return new Promise((resolve, reject) => {
        if (!element) {
            reject(new Error("元素不存在"));
            return;
        }

        if (!document.fullscreenEnabled) {
            reject(new Error("当前浏览器不支持全屏"));
            return;
        }

        if (element.requestFullscreen) {
            element.requestFullscreen().then(() => resolve());
        } else if ((element as any).mozRequestFullScreen) {
            (element as any).mozRequestFullScreen().then(() => resolve());
        } else if ((element as any).webkitRequestFullscreen) {
            (element as any).webkitRequestFullscreen().then(() => resolve());
        } else if ((element as any).msRequestFullscreen) {
            (element as any).msRequestFullscreen().then(() => resolve());
        } else {
            reject(new Error("浏览器不支持全屏API"));
        }
    });
};

/** 退出全屏 */
export const exitFullScreen = (): Promise<void> => {
    return new Promise((resolve, reject) => {
        if (!document.fullscreenElement) {
            resolve(); // 已经不是全屏状态
            return;
        }

        if (document.exitFullscreen) {
            document.exitFullscreen().then(() => resolve());
        } else if ((document as any).mozCancelFullScreen) {
            (document as any).mozCancelFullScreen().then(() => resolve());
        } else if ((document as any).webkitExitFullscreen) {
            (document as any).webkitExitFullscreen().then(() => resolve());
        } else if ((document as any).msExitFullscreen) {
            (document as any).msExitFullscreen().then(() => resolve());
        } else {
            reject(new Error("浏览器不支持退出全屏API"));
        }
    });
};

/** 检查是否全屏 */
export const isFullScreen = (element: HTMLElement | null | undefined): boolean => {
    return !!(element && (element === document.fullscreenElement));
};
